package com.bcxj.serviceprice.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bcxj.internalcommon.dto.ForecastPriceDTO;
import com.bcxj.internalcommon.dto.ResponseResult;
import com.bcxj.internalcommon.entity.PriceRule;
import com.bcxj.internalcommon.response.DirectionResponse;
import com.bcxj.internalcommon.response.ForecastPriceResponse;
import com.bcxj.internalcommon.util.BigDecimalUtils;
import com.bcxj.serviceprice.mapper.PriceRuleMapper;
import com.bcxj.serviceprice.remote.ServiceMapClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;

/**
 * ClassName: ForecastPriceService
 * Package: com.bcxj.serviceprice.service
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/10/5 15:00
 * @Version 1.0
 * @Since 1.0
 */
@Service
public class ForecastPriceService {

    @Autowired
    private ServiceMapClient serviceMapClient;

    @Autowired
    private PriceRuleMapper priceRuleMapper;

    /**
     * 预估价格
     * @param depLongitude 经度
     * @param depLatitude 纬度
     * @param destLongitude 经度
     * @param destLatitude 纬度
     * @return
     */
    public ResponseResult forecastPrice(String depLongitude, String depLatitude, String destLongitude, String destLatitude
    , String cityCode, String vehicleType) {

        ForecastPriceDTO forecastPriceDTO = ForecastPriceDTO.builder().depLongitude(depLongitude).depLatitude(depLatitude).destLongitude(destLongitude).destLatitude(destLatitude).build();

        // 调用远程服务获取距离
        ResponseResult result = serviceMapClient.driving(forecastPriceDTO);
        DirectionResponse data = (DirectionResponse)result.getData();
        // System.out.println(data);

        if(null == data){
            return ResponseResult.fail("获取距离失败");
        }

        // 获取最新的计价规则
        List<PriceRule> priceRules = priceRuleMapper.selectList(new LambdaQueryWrapper<PriceRule>()
                .eq(PriceRule::getCityCode, cityCode)
                .eq(PriceRule::getVehicleType, vehicleType)
                .orderByDesc(PriceRule::getFareVersion));

        if(priceRules.isEmpty()){
            return ResponseResult.fail("计价规则为空");
        }

        System.out.println(priceRules.get(0));

        // 计算价格
        ForecastPriceResponse forecastPriceResponse = new ForecastPriceResponse();
        forecastPriceResponse.setPrice(getPrice(data.getDistance(), data.getDuration(), priceRules.get(0)));
        forecastPriceResponse.setCityCode(cityCode);
        forecastPriceResponse.setVehicleType(vehicleType);
        forecastPriceResponse.setFareVersion(priceRules.get(0).getFareVersion());
        forecastPriceResponse.setFareType(priceRules.get(0).getFareType());
        return ResponseResult.success(forecastPriceResponse);
    }


    /**
     * 根据时间和距离计算价格
     * @param distance 米为单位
     * @param duration s 为单位
     * @param priceRule 计价规则
     * @return
     */
    private static Double getPrice(double distance, double duration, PriceRule priceRule) {
        // 加上起步价
        double price = 0;
        price = BigDecimalUtils.add(price, priceRule.getStartFare());

        // 计算里程费distanceDec.multiply(BigDecimal.valueOf(priceRule.getUnitPricePerMile()))
        distance = BigDecimalUtils.div(distance, 1000, 2);
        double distanceSubtract = BigDecimalUtils.sub(distance, priceRule.getStartMile());
        distance = distanceSubtract < 0 ? 0: distanceSubtract;

        price += BigDecimalUtils.mul(distance, priceRule.getUnitPricePerMile());

        // 计算时长费, 计价单位为s
        duration = BigDecimalUtils.div(duration, 60, 2);
        price += BigDecimalUtils.mul(duration, priceRule.getUnitPricePerMinute());


        return price;
    }


    public static void main(String[] args) {
        PriceRule priceRule = new PriceRule();
        priceRule.setStartFare(10.0);
        priceRule.setStartMile(3);
        priceRule.setUnitPricePerMile(1.8);
        priceRule.setUnitPricePerMinute(0.5);

        System.out.println(getPrice(6500, 1800, priceRule));

    }

    /**
     * 计算真实价格
     * @param distance
     * @param duration
     * @param fareType
     * @param fareVersion
     * @return
     */
    public ResponseResult<Double> calculatePrice(String distance, String duration, String fareType, String fareVersion) {
        // 获取计价规则
        LambdaQueryWrapper<PriceRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PriceRule::getFareType, fareType)
                .eq(PriceRule::getFareVersion, fareVersion);

        PriceRule priceRule = priceRuleMapper.selectOne(queryWrapper);

        // 计算价格
        Double price = getPrice(Double.parseDouble(distance), Double.parseDouble(duration) * 60, priceRule);

        return ResponseResult.success(price);
    }
}
